// 技能按钮
library SkillButtonUI initializer init requires UIInit

    // 初始创建UI
    private function createUI takes nothing returns nothing
        local integer ui = StringHash("技能按钮")
        local integer i
        local integer i2

        // 第一个按钮背景
        set i = 0
        // 格子
        call SaveInteger(UI_HASH, ui, i, DzCreateFrameByTagName("BACKDROP", "背景", DzGetGameUI(), "BACKDROP", 0))
        call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i), 1, DzFrameGetHeroBarButton(0), 7, 0, -0.02 )
        call DzFrameSetTexture( LoadInteger(UI_HASH, ui, i), "btn_gezi.tga", 0 )
        call DzFrameSetSize( LoadInteger(UI_HASH, ui, i), 0.035, 0.035 )
        call DzFrameShow( LoadInteger(UI_HASH, ui, i), true )
        // 技能图标
        set i2 = i + 1000
        call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("BACKDROP", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
        call DzFrameSetSize( LoadInteger(UI_HASH, ui, i2), 0.030, 0.030 )
        call DzFrameSetTexture( LoadInteger(UI_HASH, ui, i2), "ReplaceableTextures\\CommandButtons\\BTNPeasant.blp", 0 )
        call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0, 0.0 )
        call DzFrameShow( LoadInteger(UI_HASH, ui, i2), false )

        // 释放进行CD
        set i2 = i + 2000
        call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("SPRITE", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
        call DzFrameClearAllPoints( LoadInteger(UI_HASH, ui, i2) )
        call DzFrameSetModel( LoadInteger(UI_HASH, ui, i2), "UI-Cooldown-Indicator-green.mdx", 0, 0 )
        call DzFrameSetAnimate( LoadInteger(UI_HASH, ui, i2), 0, false )
        call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0.032, 0.026 )
        call DzFrameSetAnimateOffset( LoadInteger(UI_HASH, ui, i2), 1.0 )

        // 释放结束CD
        set i2 = i + 3000
        call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("SPRITE", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
        call DzFrameClearAllPoints( LoadInteger(UI_HASH, ui, i2) )
        call DzFrameSetModel( LoadInteger(UI_HASH, ui, i2), "UI-Cooldown-Indicator.mdx", 0, 0 )
        call DzFrameSetAnimate( LoadInteger(UI_HASH, ui, i2), 0, false )
        call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0.032, 0.026 )
        call DzFrameSetAnimateOffset( LoadInteger(UI_HASH, ui, i2), 1.0 )

        set i2 = i + 4000
        call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("GLUEBUTTON", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
        call DzFrameSetSize( LoadInteger(UI_HASH, ui, i2), 0.030, 0.030 )
        call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0, 0.0 )
        // 点击事件
        call DzFrameSetScript(LoadInteger(UI_HASH, ui, i2), 1, "CESkillButton", false)
        // 鼠标进入事件
        call DzFrameSetScript(LoadInteger(UI_HASH, ui, i2), 2, "IESkillButton", false)
        // 鼠标离开事件
        call DzFrameSetScript(LoadInteger(UI_HASH, ui, i2), 3, "OESkillButton", false)

        set i = 1
        loop
            exitwhen i >= 8 

            // 格子
            call SaveInteger(UI_HASH, ui, i, DzCreateFrameByTagName("BACKDROP", "背景", DzGetGameUI(), "BACKDROP", 0))
            call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i), 1, LoadInteger(UI_HASH, ui, i - 1), 7, 0, -0.005 )
            call DzFrameSetTexture( LoadInteger(UI_HASH, ui, i), "btn_gezi.tga", 0 )
            call DzFrameSetSize( LoadInteger(UI_HASH, ui, i), 0.035, 0.035 )
            call DzFrameShow( LoadInteger(UI_HASH, ui, i), true )
            // 技能图标
            set i2 = i + 1000
            call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("BACKDROP", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
            call DzFrameSetSize( LoadInteger(UI_HASH, ui, i2), 0.030, 0.030 )
            call DzFrameSetTexture( LoadInteger(UI_HASH, ui, i2), "ReplaceableTextures\\CommandButtons\\BTNPeasant.blp", 0 )
            call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0, 0.0 )
            call DzFrameShow( LoadInteger(UI_HASH, ui, i2), false )

            // 释放进行CD
            set i2 = i + 2000
            call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("SPRITE", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
            call DzFrameClearAllPoints( LoadInteger(UI_HASH, ui, i2) )
            call DzFrameSetModel( LoadInteger(UI_HASH, ui, i2), "UI-Cooldown-Indicator-green.mdx", 0, 0 )
            call DzFrameSetAnimate( LoadInteger(UI_HASH, ui, i2), 0, false )
            call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0.032, 0.026 )
            call DzFrameSetAnimateOffset( LoadInteger(UI_HASH, ui, i2), 1.0 )

            // 释放结束CD
            set i2 = i + 3000
            call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("SPRITE", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
            call DzFrameClearAllPoints( LoadInteger(UI_HASH, ui, i2) )
            call DzFrameSetModel( LoadInteger(UI_HASH, ui, i2), "UI-Cooldown-Indicator.mdx", 0, 0 )
            call DzFrameSetAnimate( LoadInteger(UI_HASH, ui, i2), 0, false )
            call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0.032, 0.026 )
            call DzFrameSetAnimateOffset( LoadInteger(UI_HASH, ui, i2), 1.0 )

            set i2 = i + 4000
            call SaveInteger(UI_HASH, ui, i2, DzCreateFrameByTagName("GLUEBUTTON", "name", LoadInteger(UI_HASH, ui, i), "template", 0))
            call DzFrameSetSize( LoadInteger(UI_HASH, ui, i2), 0.030, 0.030 )
            call DzFrameSetPoint( LoadInteger(UI_HASH, ui, i2), 4, LoadInteger(UI_HASH, ui, i), 4, 0, 0.0 )
            // 点击事件
            call DzFrameSetScript(LoadInteger(UI_HASH, ui, i2), 1, "CESkillButton", false)
            // 鼠标进入事件
            call DzFrameSetScript(LoadInteger(UI_HASH, ui, i2), 2, "IESkillButton", false)
            // 鼠标离开事件
            call DzFrameSetScript(LoadInteger(UI_HASH, ui, i2), 3, "OESkillButton", false)
                
            set i = i + 1
        endloop
    endfunction

    // 获取指定技能按钮的进行时模型
    function getSkillButtonModel takes integer btnId returns integer
        local integer ui = StringHash("技能按钮")
        return LoadInteger(UI_HASH, ui, btnId - 1 + 2000)
    endfunction
    // 获得指定技能按钮释放结束时的模型
    function getSkillButtonModelEnd takes integer btnId returns integer
        local integer ui = StringHash("技能按钮")
        return LoadInteger(UI_HASH, ui, btnId - 1 + 3000)
    endfunction
    // 设置指定格子的技能图标
    function setSkillButtonIconShow takes integer uid, integer btnId, string icoPath returns nothing
        local integer ui = StringHash("技能按钮")
        if Player(uid) == GetLocalPlayer() then
            call DzFrameSetTexture( LoadInteger(UI_HASH, ui, btnId - 1 + 1000), icoPath, 0 )
            call DzFrameShow( LoadInteger(UI_HASH, ui, btnId - 1 + 1000), true )
        endif
    endfunction


    private function init takes nothing returns nothing

        local integer ui = StringHash("技能按钮")
        local integer i = 0
        call createUI()

        // TODO 
        // 这里要存档各种加成完结束再设置，并且要异步处理
        // loop
        //     exitwhen i >= 8
        //     // 显示当前数量的格子
        //     if >= unlockedSkillGrid[uid] then
        //         call DzFrameShow( LoadInteger(UI_HASH, ui, i), false )
        //     endif
        //     set i = i + 1
        // endloop
    endfunction
endlibrary



